<template>
  <div class="app-container">
    <div style="display: flex; justify-content: space-between">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="addRulesClick"
            v-hasPermi="['attendance:rule:overtime:add']"
            >新增加班规则</el-button
          >
        </el-col>
      </el-row>
      <!-- 请输入加班规则名称输入框-->
      <div>
        <el-input
          v-model="queryParams.name"
          style="max-width: 200px; margin-left: 10px"
          placeholder="请输入加班规则名称"
          @change="handleQuery"
        >
          <template #append>
            <el-button :icon="Search" />
          </template>
        </el-input>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="postList"
      :default-sort="{ prop: 'postId', order: 'descending' }"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="规则名称"
        align="center"
        prop="name"
        width="180"
      />
      <el-table-column label="规则内容" align="center" prop="createTime">
        <template #default="scope">
          <!-- <span>{{ scope.row.ruleInfo }}</span> -->
          <div v-for="item in scope.row.ruleInfo" :key="item.id">
            <span v-if="item.dayType == 1">工作日：</span>
            <span v-if="item.dayType == 2">休息日：</span>
            <span v-if="item.dayType == 3">节假日：</span>
            <span v-if="item.isOvertime == 0">不允许加班</span>
            <span v-if="item.isOvertime != 0">加班必须审批，</span>
            <span v-if="item.calculateWay == 2 && item.isOvertime != 0"
              >{{ item.calculateWay }}以打卡为准，但不能超过审批单时长</span
            >
            <span v-if="item.calculateWay != 2 && item.isOvertime != 0"
              >{{ item.calculateWay }}加班时长以审批单为准</span
            >
          </div>
        </template>
      </el-table-column>

      <el-table-column label="应用范围" align="center">
        <template #default="scope">
          <!-- <span>{{ scope.row.groupIds }}</span> -->
          <span
            style="margin-left: 10px"
            v-for="item in scope.row.groupIds"
            :key="item"
            >{{ handleGroupIdsShow(item) }}</span
          >
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['attendance:rule:overtime:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['attendance:rule:overtime:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修班次对话框 -->
    <el-drawer :title="title" v-model="open" direction="rtl">
      <template #default>
        <div>
          <el-form
            :model="form2"
            label-position="top"
            label-width="auto"
            :rules="rules"
            ref="postRef"
          >
            <el-form-item label="规则名称" prop="name">
              <el-input
                v-model="form2.name"
                placeholder="请输入"
                maxlength="30"
                show-word-limit
                style="width: 300px"
              />
            </el-form-item>
            <el-form-item label="应用范围" prop="groupIds">
              <el-select
                v-model="form2.groupIds"
                filterable
                multiple
                placeholder="未设置"
                style="width: 300px"
              >
                <el-option
                  v-for="item in groupSelectList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="负责人" prop="principalId">
              <el-select
                v-model="form2.principalId"
                placeholder="未设置"
                style="width: 300px"
              >
                <el-option
                  v-for="item in principalSelectList"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-tabs v-model="activeName" class="demo-tabs">
                <el-tab-pane label="工作日" name="1">
                  <div>
                    允许加班
                    <el-switch
                      v-model="isAllowWeekday"
                      @change="handleAllowOvertime1"
                    />
                  </div>
                  <div v-if="isAllowWeekday">
                    <div class="jb">
                      <el-radio-group
                        v-model="weekdayForm.isOvertime"
                        class="ml-4"
                      >
                        <el-radio :value="1" size="large"
                          >允许班前和班后加班</el-radio
                        >
                        <el-radio :value="2" size="large"
                          >仅允许班前加班</el-radio
                        >
                        <el-radio :value="3" size="large"
                          >仅允许班后加班</el-radio
                        >
                      </el-radio-group>
                    </div>
                    <div class="xztj">
                      <p>计算方式</p>
                      <el-radio-group
                        v-model="weekdayForm.calculateWay"
                        class="ml-4"
                      >
                        <el-radio :value="1" size="large"
                          >按审批时长计算</el-radio
                        >
                        <el-radio :value="2" size="large"
                          >在审批的时段内，按打卡时长计算</el-radio
                        >
                        <el-radio :value="3" size="large"
                          >无需审批，按打卡时长计算</el-radio
                        >
                      </el-radio-group>
                    </div>
                    <div>
                      <p>加班起算时间</p>
                      <div style="margin-top: 20px">
                        下班
                        <el-input
                          type="number"
                          :min="0"
                          v-model="weekdayForm.calculateStartMinuteNum"
                          style="width: 100px"
                        />
                        分钟后开始算加班
                      </div>
                    </div>
                    <div>
                      <p>最小加班时间</p>
                      <div
                        style="margin-top: 20px"
                        v-if="
                          weekdayForm.isOvertime == 1 ||
                          weekdayForm.isOvertime == 2
                        "
                      >
                        班前加班少于
                        <el-input
                          type="number"
                          :min="0"
                          v-model="weekdayForm.frontCountMinuteNum"
                          style="width: 100px"
                        />
                        分钟，不计入加班
                      </div>
                      <div
                        style="margin-top: 20px"
                        v-if="
                          weekdayForm.isOvertime == 1 ||
                          weekdayForm.isOvertime == 3
                        "
                      >
                        班后加班少于
                        <el-input
                          type="number"
                          :min="0"
                          v-model="weekdayForm.everyCountMinuteNum"
                          style="width: 100px"
                        />
                        分钟，不计入加班
                      </div>
                      <div style="margin-top: 20px">
                        累计加班少于
                        <el-input
                          type="number"
                          :min="0"
                          v-model="weekdayForm.allCountMinuteNum"
                          style="width: 100px"
                        />
                        分钟，不计入加班
                      </div>
                      <div>
                        扣除休息时间
                        <el-switch
                          v-model="isDelrestTime1"
                          @change="handleDeductingRest1"
                        />
                      </div>
                      <div v-if="isDelrestTime1 === true" class="xztj">
                        <el-radio-group
                          v-model="weekdayForm.delRestTime"
                          class="ml-4"
                        >
                          <el-radio :value="1" size="large"
                            >按休息时段扣除</el-radio
                          >
                          <el-radio :value="2" size="large"
                            >按加班时长扣除</el-radio
                          >
                        </el-radio-group>

                        <div v-if="weekdayForm.delRestTime === 1" class="xx">
                          休息开始：<el-time-picker
                            style="width: 100px"
                            v-model="weekdayForm.startTime"
                            value-format="HH:mm"
                          />
                          休息结束：<el-time-picker
                            style="width: 100px"
                            v-model="weekdayForm.endTime"
                            value-format="HH:mm"
                          />
                        </div>

                        <div
                          v-if="weekdayForm.delRestTime === 2"
                          style="bottom: 0; position: relative"
                        >
                          每天加班满
                          <el-input
                            :min="0"
                            style="width: 80px"
                            v-model="weekdayForm.fullCountHourNum"
                            type="number"
                          />
                          小时，扣除
                          <el-input
                            :min="0"
                            style="width: 80px"
                            v-model="weekdayForm.delCountHourNum"
                            type="number"
                          />
                          小时
                        </div>
                      </div>
                    </div>

                    <div style="margin-top: 20px">
                      <div>
                        加班时长计为调休或加班费
                        <el-switch
                          v-model="isDaysOffOrOvertime1"
                          @change="handleCompensatoryLeave1"
                        />
                      </div>

                      <div v-if="isDaysOffOrOvertime1" class="xztj">
                        <el-radio-group
                          v-model="weekdayForm.daysOffOrOvertime"
                          class="ml-4"
                        >
                          <el-radio :value="1" size="large">计为调休</el-radio>

                          <el-radio :value="2" size="large"
                            >计为加班费</el-radio
                          >
                        </el-radio-group>

                        <div v-if="weekdayForm.daysOffOrOvertime === 1">
                          按照1：
                          <el-input
                            :min="0"
                            type="number"
                            style="width: 80px"
                            v-model="weekdayForm.daysOffRatio"
                          />
                          计为调休时长，并关联假期项：调休
                        </div>
                      </div>
                    </div>
                  </div>
                </el-tab-pane>
                <el-tab-pane label="休息日" name="2">
                  <div>
                    允许加班
                    <el-switch
                      v-model="isAllowRestday"
                      @change="handleAllowOvertime2"
                    />
                  </div>
                  <div v-if="isAllowRestday">
                    <div class="xztj">
                      <p>计算方式</p>
                      <el-radio-group
                        v-model="restDayForm.calculateWay"
                        class="ml-4"
                      >
                        <el-radio :value="1" size="large"
                          >按审批时长计算</el-radio
                        >
                        <el-radio :value="2" size="large"
                          >在审批的时段内，按打卡时长计算</el-radio
                        >
                        <el-radio :value="3" size="large"
                          >无需审批，按打卡时长计算</el-radio
                        >
                      </el-radio-group>
                    </div>
                    <div>
                      <p>最小加班时间</p>
                      <div style="margin-top: 20px">
                        累计加班少于
                        <el-input
                          type="number"
                          :min="0"
                          v-model="restDayForm.allCountMinuteNum"
                          style="width: 100px"
                        />
                        分钟，不计入加班
                      </div>
                      <div style="margin-top: 10px">
                        扣除休息时间
                        <el-switch
                          v-model="isDelrestTime2"
                          @change="handleDeductingRest2"
                        />
                      </div>
                      <div v-if="isDelrestTime2 === true" class="xztj">
                        <el-radio-group
                          v-model="restDayForm.delRestTime"
                          class="ml-4"
                        >
                          <el-radio :value="1" size="large"
                            >按休息时段扣除</el-radio
                          >
                          <el-radio :value="2" size="large"
                            >按加班时长扣除</el-radio
                          >
                        </el-radio-group>

                        <div v-if="restDayForm.delRestTime === 1" class="xx">
                          休息开始：<el-time-picker
                            style="width: 100px"
                            v-model="restDayForm.startTime"
                            value-format="HH:mm"
                          />
                          休息结束：<el-time-picker
                            style="width: 100px"
                            v-model="restDayForm.endTime"
                            value-format="HH:mm"
                          />
                        </div>

                        <div
                          v-if="restDayForm.delRestTime === 2"
                          style="bottom: 0; position: relative"
                        >
                          每天加班满
                          <el-input
                            :min="0"
                            style="width: 80px"
                            v-model="restDayForm.fullCountHourNum"
                            type="number"
                          />
                          小时，扣除
                          <el-input
                            :min="0"
                            style="width: 80px"
                            v-model="restDayForm.delCountHourNum"
                            type="number"
                          />
                          小时
                        </div>
                      </div>
                    </div>
                    <div style="margin-top: 20px">
                      <div>
                        加班时长计为调休或加班费
                        <el-switch
                          v-model="isDaysOffOrOvertime2"
                          @change="handleCompensatoryLeave2"
                        />
                      </div>
                      <div v-if="isDaysOffOrOvertime2" class="xztj">
                        <el-radio-group
                          v-model="restDayForm.daysOffOrOvertime"
                          class="ml-4"
                        >
                          <el-radio :value="1" size="large">计为调休</el-radio>
                          <el-radio :value="2" size="large"
                            >计为加班费</el-radio
                          >
                          <el-radio
                            v-if="restDayForm.calculateWay !== 3"
                            :value="3"
                            size="large"
                            >员工自主选择</el-radio
                          >
                        </el-radio-group>
                        <div
                          v-if="
                            restDayForm.daysOffOrOvertime === 1 ||
                            restDayForm.daysOffOrOvertime === 3
                          "
                        >
                          按照1：
                          <el-input
                            :min="0"
                            type="number"
                            style="width: 80px"
                            v-model="restDayForm.daysOffRatio"
                          />
                          计为调休时长，并关联假期项：调休
                        </div>
                      </div>
                    </div>
                  </div>
                </el-tab-pane>
                <el-tab-pane label="节假日" name="3">
                  <div>
                    允许加班
                    <el-switch
                      v-model="isAllowHoliday"
                      @change="handleAllowOvertime3"
                    />
                  </div>
                  <div v-if="isAllowHoliday">
                    <div class="xztj">
                      <p>计算方式</p>
                      <el-radio-group
                        v-model="holidayVacationsForm.calculateWay"
                        class="ml-4"
                      >
                        <el-radio :value="1" size="large"
                          >按审批时长计算</el-radio
                        >
                        <el-radio :value="2" size="large"
                          >在审批的时段内，按打卡时长计算</el-radio
                        >
                        <el-radio :value="3" size="large"
                          >无需审批，按打卡时长计算</el-radio
                        >
                      </el-radio-group>
                    </div>
                    <div>
                      <p>最小加班时间</p>
                      <div style="margin-top: 20px">
                        累计加班少于
                        <el-input
                          type="number"
                          :min="0"
                          v-model="holidayVacationsForm.allCountMinuteNum"
                          style="width: 100px"
                        />
                        分钟，不计入加班
                      </div>
                      <div style="margin-top: 10px">
                        扣除休息时间
                        <el-switch
                          v-model="isDelrestTime3"
                          @change="handleDeductingRest3"
                        />
                      </div>
                      <div v-if="isDelrestTime3 === true" class="xztj">
                        <el-radio-group
                          v-model="holidayVacationsForm.delRestTime"
                          class="ml-4"
                        >
                          <el-radio :value="1" size="large"
                            >按休息时段扣除</el-radio
                          >
                          <el-radio :value="2" size="large"
                            >按加班时长扣除</el-radio
                          >
                        </el-radio-group>

                        <div
                          v-if="holidayVacationsForm.delRestTime === 1"
                          class="xx"
                        >
                          休息开始：<el-time-picker
                            style="width: 100px"
                            v-model="holidayVacationsForm.startTime"
                            value-format="HH:mm"
                          />
                          休息结束：<el-time-picker
                            style="width: 100px"
                            v-model="holidayVacationsForm.endTime"
                            value-format="HH:mm"
                          />
                        </div>

                        <div
                          v-if="holidayVacationsForm.delRestTime === 2"
                          style="bottom: 0; position: relative"
                        >
                          每天加班满
                          <el-input
                            :min="0"
                            style="width: 80px"
                            v-model="holidayVacationsForm.fullCountHourNum"
                            type="number"
                          />
                          小时，扣除
                          <el-input
                            :min="0"
                            style="width: 80px"
                            v-model="holidayVacationsForm.delCountHourNum"
                            type="number"
                          />
                          小时
                        </div>
                      </div>
                    </div>
                    <div style="margin-top: 20px">
                      <div>
                        加班时长计为调休或加班费
                        <el-switch
                          v-model="isDaysOffOrOvertime3"
                          @change="handleCompensatoryLeave3"
                        />
                      </div>
                      <div v-if="isDaysOffOrOvertime3" class="xztj">
                        <el-radio-group
                          v-model="holidayVacationsForm.daysOffOrOvertime"
                          class="ml-4"
                        >
                          <el-radio :value="1" size="large">计为调休</el-radio>
                          <el-radio :value="2" size="large"
                            >计为加班费</el-radio
                          >
                          <el-radio
                            v-if="holidayVacationsForm.calculateWay !== 3"
                            :value="3"
                            size="large"
                            >员工自主选择</el-radio
                          >
                        </el-radio-group>
                        <div
                          v-if="
                            holidayVacationsForm.daysOffOrOvertime === 1 ||
                            holidayVacationsForm.daysOffOrOvertime === 3
                          "
                        >
                          按照1：
                          <el-input
                            :min="0"
                            type="number"
                            style="width: 80px"
                            v-model="holidayVacationsForm.daysOffRatio"
                          />
                          计为调休时长，并关联假期项：调休
                        </div>
                      </div>
                    </div>
                  </div>
                </el-tab-pane>
              </el-tabs>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="Rule">
import {
  getOvertimeListApi,
  getGroupListApi,
  addOverTimeApi,
  deleteOvertimeApi,
  getOvertimeByIdApi,
  putOvertimeApi,
} from "@/api/attendance/attendance";
import { getOrgSearchAgApi } from "@/api/attendance/shift";
import { Search } from "@element-plus/icons-vue";
import { ref } from "vue";
const { proxy } = getCurrentInstance();
// const { sys_normal_disable } = proxy.useDict('sys_normal_disable')

const isAllowWeekday = ref(false);
const isAllowRestday = ref(false);
const isAllowHoliday = ref(false);

const isDelrestTime1 = ref(false);
const isDelrestTime2 = ref(false);
const isDelrestTime3 = ref(false);

const isDaysOffOrOvertime1 = ref(false);
const isDaysOffOrOvertime2 = ref(false);
const isDaysOffOrOvertime3 = ref(false);

// 工作日
const weekdayForm = ref({
  dayType: 1,
  isOvertime: 1, //是否允许加班 0：否(isAllowWeekday==false) 1：允许班前和班后加班 2：仅允许班前加班 3：仅允许班后加班
  calculateWay: 1, //计算方式 1：按审批时长计算 2：在审批的时段内，按打卡时长计算 3：无需审批，按打卡时长计算
  calculateStartMinuteNum: undefined, //加班起算时间 下班xx分钟后开始算加班
  frontCountMinuteNum: undefined, //班前加班少于xx分钟，不计入加班
  everyCountMinuteNum: undefined, //班后加班少于xx分钟，不计入加班
  allCountMinuteNum: undefined, //累计加班少于xx分钟，不计入加班
  delRestTime: 0, //扣除休息时间 0:否(isDelrestTime1==false) 1：按休息时段扣除 2：按加班时长扣除
  startTime: undefined, //休息开始
  endTime: undefined, //休息结束
  fullCountHourNum: undefined, //每天加满
  delCountHourNum: undefined, //每天扣除
  daysOffOrOvertime: 0, //扣除休息时间 0:否 1：按休息时段扣除 2：按加班时长扣除
  daysOffRatio: undefined, //调休比例
});
// 休息日
const restDayForm = ref({
  dayType: 2,
  isOvertime: 0, //是否允许加班 0：否 1：是
  calculateWay: 1, //计算方式 1：按审批时长计算 2：在审批的时段内，按打卡时长计算 3：无需审批，按打卡时长计算
  allCountMinuteNum: undefined, //累计加班少于xx分钟，不计入加班
  delRestTime: 0, //扣除休息时间 0:否(isDelrestTime2==false) 1：按休息时段扣除 2：按加班时长扣除
  startTime: undefined, //休息开始
  endTime: undefined, //休息结束
  fullCountHourNum: undefined, //每天加满
  delCountHourNum: undefined, //每天扣除
  daysOffOrOvertime: 0, //扣除休息时间 0:否 1：按休息时段扣除 2：按加班时长扣除 3：员工自主选择
  daysOffRatio: undefined, //调休比例
});
// 节假日
const holidayVacationsForm = ref({
  dayType: 3,
  isOvertime: 0, //是否允许加班 0：否 1：是
  calculateWay: 1, //计算方式 1：按审批时长计算 2：在审批的时段内，按打卡时长计算 3：无需审批，按打卡时长计算
  allCountMinuteNum: undefined, //累计加班少于xx分钟，不计入加班
  delRestTime: 0, //扣除休息时间 0:否(isDelrestTime3==false) 1：按休息时段扣除 2：按加班时长扣除
  startTime: undefined, //休息开始
  endTime: undefined, //休息结束
  fullCountHourNum: undefined, //每天加满
  delCountHourNum: undefined, //每天扣除
  daysOffOrOvertime: 0, //扣除休息时间 0:否 1：按休息时段扣除 2：按加班时长扣除 3：员工自主选择
  daysOffRatio: undefined, //调休比例
});

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const total = ref(0);
const title = ref("");
// 加班规则默认展示
const activeName = ref("1");
const data = reactive({
  form2: {
    name: "", //规则名称
    groupIds: "", //应用范围
    principalId: "",
    overtimeRuleInfos: [], //加班规则的集合
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: "",
  },
  rules: {
    name: [{ required: true, message: "规则名称不能为空", trigger: "blur" }],
  },
});

const { queryParams, rules, form2 } = toRefs(data);

/**
 * 重置需补卡部分的参数
 */
const resetDepartment = (val) => {
  if (val == 1) {
    weekdayForm.value.calculateWay = undefined;
    weekdayForm.value.calculateStartMinuteNum = undefined;
    weekdayForm.value.frontCountMinuteNum = undefined;
    weekdayForm.value.everyCountMinuteNum = undefined;
    weekdayForm.value.allCountMinuteNum = undefined;
    weekdayForm.value.delRestTime = undefined;
    weekdayForm.value.startTime = undefined;
    weekdayForm.value.endTime = undefined;
    weekdayForm.value.fullCountHourNum = undefined;
    weekdayForm.value.delCountHourNum = undefined;
    weekdayForm.value.daysOffOrOvertime = undefined;
    weekdayForm.value.daysOffRatio = undefined;
  }
  if (val == 2) {
    restDayForm.value.calculateWay = undefined;
    restDayForm.value.allCountMinuteNum = undefined;
    restDayForm.value.delRestTime = undefined;
    restDayForm.value.startTime = undefined;
    restDayForm.value.endTime = undefined;
    restDayForm.value.fullCountHourNum = undefined;
    restDayForm.value.delCountHourNum = undefined;
    restDayForm.value.daysOffOrOvertime = undefined;
    restDayForm.value.daysOffRatio = undefined;
  }
  if (val == 3) {
    holidayVacationsForm.value.calculateWay = undefined;
    holidayVacationsForm.value.allCountMinuteNum = undefined;
    holidayVacationsForm.value.delRestTime = undefined;
    holidayVacationsForm.value.startTime = undefined;
    holidayVacationsForm.value.endTime = undefined;
    holidayVacationsForm.value.fullCountHourNum = undefined;
    holidayVacationsForm.value.delCountHourNum = undefined;
    holidayVacationsForm.value.daysOffOrOvertime = undefined;
    holidayVacationsForm.value.daysOffRatio = undefined;
  }
};

watch(
  () => isAllowWeekday.value,
  (newValue) => {
    if (newValue == true) {
      weekdayForm.value.isOvertime = 1;
    } else if (newValue == false) {
      weekdayForm.value.isOvertime = 0;
      resetDepartment(1);
    }
  },
  { deep: true, immediate: true }
);
watch(
  () => isAllowRestday.value,
  (newValue) => {
    if (newValue == true) {
      restDayForm.value.isOvertime = 1;
    } else if (newValue == false) {
      restDayForm.value.isOvertime = 0;
      resetDepartment(2);
    }
  },
  { deep: true, immediate: true }
);
watch(
  () => isAllowHoliday.value,
  (newValue) => {
    if (newValue == true) {
      holidayVacationsForm.value.isOvertime = 1;
    } else if (newValue == false) {
      holidayVacationsForm.value.isOvertime = 0;
      resetDepartment(3);
    }
  },
  { deep: true, immediate: true }
);

/** 加班规则分页列表 */
function getList() {
  loading.value = true;
  getOvertimeListApi(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form2.value = {
    name: "",
    groupIds: "",
    overtimeRuleInfos: [],
    principalId: "",
  };

  proxy.resetForm("postRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

const groupSelectList = ref([]);
const principalSelectList = ref([]);
/**
 * 获取所有考勤组名称和id
 */
const getGroupList = () => {
  getGroupListApi().then((res) => {
    groupSelectList.value = res.data;
  });
};

/**
 * 获取有管理考勤打卡资格的用户信息
 */
const getOrgSearchAg = () => {
  getOrgSearchAgApi().then((res) => {
    principalSelectList.value = res.data;
  });
};
getOrgSearchAg();

/** 新增按钮操作 */
function addRulesClick() {
  reset();
  open.value = true;
  title.value = "新增加班规则";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const postId = row.id;
  getOvertimeByIdApi(postId).then((response) => {
    form2.value = {
      id: response.data.id,
      name: response.data.name,
      groupIds:
        response.data.groupIds && response.data.groupIds.map((v) => String(v)),
      principalId: response.data.principalId,
      overtimeRuleInfos: response.data.ruleInfo,
    };
    form2.value.overtimeRuleInfos.forEach((item) => {
      if (item.dayType == 1) {
        item.isOvertime == 0
          ? (isAllowWeekday.value = false)
          : (isAllowWeekday.value = true);

        weekdayForm.value = item;

        item.delRestTime == 0
          ? (isDelrestTime1.value = false)
          : (isDelrestTime1.value = true);

        item.delRestTime = Number(item.delRestTime);

        item.daysOffOrOvertime == 0
          ? (isDaysOffOrOvertime1.value = false)
          : (isDaysOffOrOvertime1.value = true);
        item.daysOffOrOvertime = Number(item.daysOffOrOvertime);
      }

      if (item.dayType == 2) {
        item.isOvertime == 0
          ? (isAllowRestday.value = false)
          : (isAllowRestday.value = true);
        restDayForm.value = item;
        item.delRestTime == 0
          ? (isDelrestTime2.value = false)
          : (isDelrestTime2.value = true);

        item.delRestTime = Number(item.delRestTime);
        item.daysOffOrOvertime == 0
          ? (isDaysOffOrOvertime2.value = false)
          : (isDaysOffOrOvertime2.value = true);
        item.daysOffOrOvertime = Number(item.daysOffOrOvertime);
      }

      if (item.dayType == 3) {
        item.isOvertime == 0
          ? (isAllowHoliday.value = false)
          : (isAllowHoliday.value = true);
        holidayVacationsForm.value = item;
        item.delRestTime == 0
          ? (isDelrestTime3.value = false)
          : (isDelrestTime3.value = true);

        item.delRestTime = Number(item.delRestTime);
        item.daysOffOrOvertime == 0
          ? (isDaysOffOrOvertime3.value = false)
          : (isDaysOffOrOvertime3.value = true);
        item.daysOffOrOvertime = Number(item.daysOffOrOvertime);
      }
    });
    open.value = true;
    title.value = "编辑加班规则";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form2.value.id != undefined) {
        putOvertimeApi(form2.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        form2.value.overtimeRuleInfos.push(
          weekdayForm.value,
          restDayForm.value,
          holidayVacationsForm.value
        );
        addOverTimeApi(form2.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.id;
  proxy.$modal
    .confirm('是否确认删该规则"' + row.name + '"的数据项？')
    .then(function () {
      return deleteOvertimeApi(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/**
 * 允许加班
 * @param
 */
const handleAllowOvertime1 = () => {
  if (isAllowWeekday.value == false) {
    weekdayForm.value.isOvertime = 0;
  } else {
    weekdayForm.value.isOvertime = 1;
  }
};
const handleAllowOvertime2 = () => {
  if (isAllowRestday.value == false) {
    restDayForm.value.isOvertime = 0;
  } else if (restDayForm.value.isOvertime == true) {
    restDayForm.value.isOvertime = 1;
  }
};
const handleAllowOvertime3 = () => {
  if (isAllowHoliday.value == false) {
    holidayVacationsForm.value.isOvertime = 0;
  } else if (holidayVacationsForm.value.isOvertime == true) {
    holidayVacationsForm.value.isOvertime = 1;
  }
};

/**
 * 扣除休息时间
 */
const handleDeductingRest1 = () => {
  if (isDelrestTime1.value == false) {
    weekdayForm.value.delRestTime = 0;
  } else {
    weekdayForm.value.delRestTime = 1;
  }
};
const handleDeductingRest2 = () => {
  if (isDelrestTime2.value == false) {
    restDayForm.value.delRestTime = 0;
  } else {
    restDayForm.value.delRestTime = 1;
  }
};
const handleDeductingRest3 = () => {
  if (isDelrestTime3.value == false) {
    holidayVacationsForm.value.delRestTime = 0;
  } else {
    holidayVacationsForm.value.delRestTime = 1;
  }
};

/**
 * 加班时长或调休
 */
const handleCompensatoryLeave1 = () => {
  if (isDaysOffOrOvertime1.value == false) {
    weekdayForm.value.daysOffOrOvertime = 0;
  } else {
    weekdayForm.value.daysOffOrOvertime = 1;
  }
};
const handleCompensatoryLeave2 = () => {
  if (isDaysOffOrOvertime2.value == false) {
    restDayForm.value.daysOffOrOvertime = 0;
  } else {
    restDayForm.value.daysOffOrOvertime = 1;
  }
};
const handleCompensatoryLeave3 = () => {
  if (isDaysOffOrOvertime3.value == false) {
    holidayVacationsForm.value.daysOffOrOvertime = 0;
  } else {
    holidayVacationsForm.value.daysOffOrOvertime = 1;
  }
};

/**
 * 计算表格应用范围
 * @param val
 */
const handleGroupIdsShow = (val) => {
  return (
    groupSelectList.value.filter((item) => item.id == val)[0] &&
    groupSelectList.value.filter((item) => item.id == val)[0].name
  );
};

getGroupList();
getList();
</script>
<style scoped>
.el-form-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
:deep(.el-drawer__header) {
  border-bottom: 1px solid gainsboro;
  height: 60px;
  margin-bottom: -5px;
}
:deep(.el-dialog__header) {
  margin-top: -25px;
}
.xztj {
  display: flex;
  flex-direction: column;
  margin-top: 10px;
  :deep(.el-radio-group) {
    display: flex !important;
    flex-direction: column;
    align-items: flex-start !important;
  }
}
.xx {
  position: relative;
  /* top: -80px; */
}
</style>
